﻿using SmartHRMS.Model.Account;
using SmartHRMS.Presentation.Account;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace SmartHRMS.UI.Web.Modules.Administration.Account
{
    public partial class RoleDetail : System.Web.UI.UserControl,IModuleListView,IRoleView
    {
        private ModuleListPresenter _Presenter;

        private RolePresenter _RolePresenter;

        private IList<Module> localModuleList=new List<Module>();

        private IList<string> localPermission = new List<string>();

        private string localInsertedBy;

        private string localUpdatedBy;

        protected void Page_Init(object sender, EventArgs e)
        {
            _Presenter = new ModuleListPresenter(this);
            _RolePresenter = new RolePresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _Presenter.Display();
                _RolePresenter.Display();
                PopulateTree(TreeViewPermissions.Nodes, localModuleList);
            }
        }

        #region IModuleListView Members

        public IList<Module> Module
        {
            set { localModuleList = value; }
        }

        #endregion

        /// <summary>
        /// Bind the tree view of parent hierarchy data.
        /// </summary>
        public void PopulateTree(TreeNodeCollection allItems,IList<Module> allModules)
        {
            foreach (Module modules in allModules)
            {
                TreeNode t = new TreeNode(modules.Description.ToString());
                t.SelectAction = TreeNodeSelectAction.Expand;
                t.ShowCheckBox = false;
                allItems.Add(t);
                if (modules.Permission.Count > 0)
                {
                    foreach (Permission ds in modules.Permission)
                    {
                        TreeNode d = new TreeNode(ds.PermissionKey.ToString(), ds.PermissionId.ToString());
                        d.SelectAction = TreeNodeSelectAction.Expand;
                        t.ChildNodes.Add(d);
                    }
                }

            }

                if (this.Permission.Count > 0)
                {
                    foreach (TreeNode tn in TreeViewPermissions.Nodes)
                    {
                        if (tn.Value != null)
                        {
                            if (tn.ChildNodes.Count > 0)
                            {
                                foreach (TreeNode tnc in tn.ChildNodes)
                                {
                                    foreach (string pr in this.Permission)
                                    {
                                        if (tnc.Value.ToString() == pr)
                                        {
                                            tnc.Checked = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
        }

        protected void TreeViewPermissions_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
        }

        protected void TreeViewPermissions_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
        }

        #region IRoleView Members

        public Guid RoleId
        {
            get
            {
                Guid selectedUserId;
                string id = Request.QueryString["id"].ToString();
                if (string.IsNullOrEmpty(id) || id=="0")
                {
                    selectedUserId = Guid.Empty;
                }
                else
                {
                    selectedUserId = new Guid(id);
                }

                return selectedUserId;
            }
        }

        public string Name
        {
            get
            {
                return txtRoleName.Text;
            }
            set
            {
                txtRoleName.Text = value; ;
            }
        }

        public string InsertedBy
        {
            get
            {
                return localInsertedBy;
            }
            set
            {
                localInsertedBy = value;
                HyperLinkCreatedBy.NavigateUrl = "RoleDetailPage.aspx?id=" + value;
            }
        }

        public string InsertedDate
        {
            get
            {
                return DateTime.Now.ToString();
            }
            set
            {
                txtCreatedDate.Text = value;
            }
        }

        public string UpdatedBy
        {
            get
            {
                return localUpdatedBy;
            }
            set
            {
                localUpdatedBy = value;
                HyperLinkUpdated.NavigateUrl = "RoleDetailPage.aspx?id=" + value;
            }
        }

        public string UpdatedDate
        {
            get
            {
                return DateTime.Now.ToString();
            }
            set
            {
                txtUpdatedDate.Text = value;
            }
        }

        public string InsertedByName
        {
            get
            {
                return HyperLinkCreatedBy.Text;
            }
            set
            {
                HyperLinkCreatedBy.Text = value;
            }
        }

        public string UpdatedByName
        {
            get
            {
                return HyperLinkUpdated.Text;
            }
            set
            {
                HyperLinkUpdated.Text = value;
            }
        }

        public IList<string> Permission
        {
            get
            {
                return localPermission;
            }
            set
            {
                localPermission = value;
            }
        }

        public string SaveButtonText
        {
            set { btnSave.Text = value; }
        }

        #endregion

        #region IBaseView Members

        public void ErrorMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlError.Visible = false;
            }
            else
            {
                pnlError.Visible = true;
            }
            this.litErrorMessage.Text = Message + "<br/>";
        }

        public void InfoMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlInfo.Visible = false;
            }
            else
            {
                pnlInfo.Visible = true;
            }
            this.litInfoMessage.Text = Message + "<br/>";
        }

        public void WarringMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlWarr.Visible = false;
            }
            else
            {
                pnlWarr.Visible = true;
            }
            this.litWarrMessage.Text = Message + "<br/>";
        }

        public void ViewTitle(string title)
        {
            //throw new NotImplementedException();
        }

        #endregion


        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Permission = new List<string>();
            foreach (TreeNode tn in TreeViewPermissions.Nodes)
            {
                if (tn.Value != null)
                {
                    if (tn.ChildNodes.Count>0)
                    {
                        foreach (TreeNode tnc in tn.ChildNodes)
                        {
                            if (tnc.Checked == true)
                            {
                                this.Permission.Add(tnc.Value.ToString() + "," + tn.Text+"-"+tnc.Text);
                            }
                        }
                    }
                }
            }
            _RolePresenter.SaveRole();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("RoleListPage.aspx");
        }
    }
}